package com.example.hubu20250620.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hubu20250620.entity.Movie;
import com.example.hubu20250620.entity.PageData;
import com.example.hubu20250620.mapper.MovieMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MovieService extends ServiceImpl<MovieMapper, Movie> {
    LambdaQueryWrapper<Movie> queryWrapper=null;
    //获取8条电影数据
    public List<Movie> getMovieList()
    {
        //使用lambdaQueryWrapper对象来创建查询条件，lambdaQueryWrapper是由mybatisplus提供的
        //lambdaQueryWrapper可以动态进行sql拼接，各种查询条件只需要调用对应的方法即可
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.last("limit 8");
        return super.list(queryWrapper);
    }
    //根据播放量获取前五条电影数据
    public List<Movie> getMovieListByPlayNumber(){
        queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Movie::getPlayNumber);
        queryWrapper.last("limit 5");
        return super.list(queryWrapper);
    }
    //根据电影id获取电影数据
    public Movie getMovieById(Integer movieId){
        queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Movie::getId,movieId);
        return super.getOne(queryWrapper);
    }
    //根据电影名称获取电影分页数据
//    public Page<Movie> getMoviePageList(PageData pageData, String movieName){
//        //如果movieName不为空，也不为‘’,则根据电影名称进行模糊查询
//        queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.like(("".equals(movieName)&movieName!=null),Movie::getMovieName,movieName);
//        Page<Movie> page = new Page<>(pageData.getPageCurrent(),pageData.getPageSize());
//        return this.page(page,queryWrapper);
//    }
    public Page<Movie> getMoviePageList(PageData pageData, String movieName) {
        LambdaQueryWrapper<Movie> queryWrapper = new LambdaQueryWrapper<>();

        // 修正条件判断：先检查不为null，再检查不为空字符串
        if (movieName != null && !"".equals(movieName.trim())) {
            queryWrapper.like(Movie::getMovieName, movieName);
        }

        Page<Movie> page = new Page<>(pageData.getPageCurrent(), pageData.getPageSize());
        return this.page(page, queryWrapper);
    }
}
